<?php
/**
 * 创建群组
 */
namespace App\Service;

use App\Model\Group;
use App\Model\GroupUser;
use App\ServiceInterface;

class GroupService implements ServiceInterface
{
    private $_server;

    private $_fd;

    private $_data;

    /**
     * BindService constructor.
     * @param null $server
     * @param int $fd
     * @param array $data
     */
    public function __construct($server=null,int $fd=0,array $data=[])
    {
        $this->_server = $server;
        $this->_fd     = $fd;
        $this->_data   = $data;
    }

    /**
     * 参数校验
     * @return string
     */
    public function check():string
    {
        // 判断行为动作
        if (!isset($this->_data['action']) || empty($this->_data['action']))
        {
            return 'action is null';
        }

        if (!isset($this->_data['account']) || empty($this->_data['account']))
        {
            return 'account is null';
        }
        return '';
    }

    /**
     * 具体逻辑
     * @return array
     */
    public function init():array
    {
        try {
            $check_msg = $this->check();
            if ($check_msg)
                throw new \Exception($check_msg);

            switch ($this->_data['action'])
            {
                // 创建群组
                case 'create':
                    $res_data = $this->createGroup();
                    break;
                case 'add':
                    $res_data = $this->addGroup();
                    break;
                case 'getMsg':
                    $res_data = $this->getMsgGroup();
                    break;
                case 'msgRead': // 更新最后已读的群消息
                    $res_data = $this->msgRead();
                    break;
                default:
                    $res_data = 'action not defind';
                    break;
            }

            if ($res_data && !is_array($res_data))
                throw new \Exception($res_data);

            return [0, 'success', $res_data];
        }catch (\Exception $e){
            return [-1, $e->getMessage()];
        }
    }

    /**
     * 创建群组
     * @return array|string
     */
    private function createGroup()
    {
        return Group::createGroup($this->_data['appid'], $this->_data);
    }

    /**
     * 加入群组
     * @return string
     */
    private function addGroup()
    {
        if (!isset($this->_data['accounts']) || empty($this->_data['accounts']) || !is_array($this->_data['accounts']))
        {
            return 'accounts is null or empty';
        }
        if (isset($this->_data['g_id']) || intval($this->_data['g_id']) <= 0)

        return GroupUser::addGroup((int)$this->_data['g_id'], $this->_data['accounts']);
    }

    /**
     * 获取群消息列表
     * @return array|string
     */
    private function getMsgGroup()
    {
        $this->msgRead();

        $data = Group::getMsgGroup($this->_data);
        if (is_array($data))
        {
            $data = array_reverse($data);
        }
        return $data;
    }

    private function msgRead()
    {
        return GroupUser::updateRead($this->_data['appid'], $this->_data['account'],$this->_data['g_id']);
    }

}